<?php
/**
 * PHP Class
 *
 * LICENSE
 *
 * Processing: Module helper
 *
 * @category   	Gnn Cms
 * @package    	Module Helper
 * @copyright  	Copyright vinanewstar.vn@gmail.com
 * @author		GiangNam (vinanewstar.vn@gmail.com)
 * @license    	GNU General Public License Version 2 or later (the "GPL")
 * @version     v1.0 Jun 27, 2013 - 3:06:36 PM
 */
class Zendvn_Modules_Helper{
	
	public static $listModule;
	public $gcView;
	public $gcRequest;
	public $gcTemplate;
	public $gcPage;
	
	private $_positionFromTemplate;
	
	private $_moduleList;
	private $_options;
	
	/**
	 * 
	 * @param Zendvn_Template $template
	 * @param Zendvn_Page $page
	 */
	public function __construct(Zendvn_Template &$template, Zendvn_Page $page){
		$this->gcView   		= $template->gcView;
		$this->gcTemplate 		= $template;
		$this->gcPage 			= $page;
		$this->loadAvaiableModulePositionFromTemplate();
		$this->loadModuleObjectList();
		$this->render();
		
	}
	
	/**
	 * load all avaiable module position in template
	 */
	public function loadAvaiableModulePositionFromTemplate(){
		$pos = array();
		$content = $this->gcTemplate->content;
		while(strpos($content,'{include {"type":"modules"')!=false){
			$i1 	= strpos($content,'{include ');
			$i2 	= strpos($content,' include}');
			$pos[] 	= substr($content,$i1,$i2-$i1+9);
			$content 	= substr($content,$i2+9);
		}
		if(count($pos) >0)foreach ($pos as $flagStr){
			$end = strpos($flagStr, 'include}');
			$options[$flagStr] = json_decode(trim(substr($flagStr, 8,$end-8)));
		}
		$this->_positionFromTemplate = $options;
	}
	
	/**
	 * load module (that active) list from database
	 */
	public function loadModuleObjectList(){
		// position conditions
		foreach ($this->_positionFromTemplate as $options) {
			$pos[]= '"'.$options->position.'"';
		}
		$pos = implode(",",$pos);
		
		//get moduleid list of the page
		$db = Zend_Registry::get(REGISTRY_KEY_DBO);
		$sql2 = $db ->select()
					->from(TBL_PRF."modules_page",array("moduleid"))
				    ->where("pageid = ?", $this->gcPage->id);
		$ids = implode(",", $db->fetchAll($sql2,array(),Zend_Db::FETCH_COLUMN));
		
		$sql = $db  ->select()
					->from(TBL_PRF.'modules', array("id"))
					->where('published = 1')
					->where('client_id = ?',$this->gcPage->client_id)
					->where('position IN('.$pos.')')
					->where('id IN ('.$ids.')')
					->order('ordering ASC');
		$module_ids = $db->fetchAll($sql,array(),Zend_Db::FETCH_COLUMN);
		foreach ($module_ids as $id){
			$module = new Zendvn_Modules_Module($id);
			$module ->loadContent($this->gcTemplate);
			$this->_moduleList[] = $module;
		}
	}
	/**
	 * render module
	 * @return Ambigous <unknown, mixed>
	 */
	public function render(){
		foreach($this->_positionFromTemplate AS $positionAlias => $options) {
			$allModContentAtPosition = "";
			if(count($this->_moduleList)>0)foreach($this->_moduleList AS $module){
				if($module->position == $options->position) $allModContentAtPosition .= $module->content;
			}
			$this->gcTemplate->content = str_replace($positionAlias,$allModContentAtPosition,$this->gcTemplate->content);
		} // end foreach
	}
}